package com.xcc.dataStructures.demo12_avl;

/**
 * 平衡二叉树
 */
public class AVLTreeDemo {

    public static void main(String[] args) {
//        int[] arr = {10, 11, 7, 6, 8, 9};
        int[] arr = {2, 1, 6, 5, 7, 3};

        AVLTree tree = new AVLTree();
        for (int i = 0; i < arr.length; i++) {
            tree.add(new Node(arr[i]));
        }

        System.out.println("中序遍历");
        tree.midOrder();
        System.out.println("根结点为：" + tree.getRoot());
        System.out.println("二叉树高度为：" + tree.height());
        System.out.println("左子树高度为：" + tree.leftHeight());
        System.out.println("右子树高度为：" + tree.rightHeight());
    }

}

class AVLTree {

    private Node root;

    public Node getRoot() {
        return root;
    }

    /**
     * 右子树高度
     */
    public int rightHeight() {
        return root == null ? 0 : root.rightHeight();
    }

    /**
     * 左子树高度
     */
    public int leftHeight() {
        return root == null ? 0 : root.leftHeight();
    }

    /**
     * 二叉树高度
     */
    public int height() {
        return root == null ? 0 : root.height();
    }

    /**
     * 添加
     */
    public void add(Node node) {
        if (root == null) {
            root = node;
            return;
        }
        root.add(node);
    }

    /**
     * 中序遍历
     */
    public void midOrder() {
        if (root == null) {
            System.out.println("二叉树为空，不能遍历");
            return;
        }
        root.midOrder();
    }

}

class Node {
    int value;
    Node left;
    Node right;

    /**
     * 左子树高度
     */
    public int leftHeight() {
        return this.left == null ? 0 : this.left.height();
    }

    /**
     * 右子树高度
     */
    public int rightHeight() {
        return this.right == null ? 0 : this.right.height();
    }

    /**
     * 二叉树的总高度
     */
    public int height() {
        //取左子树和右子树的最大值为总高度。左子树递归查找，如果为null则是0，不为空则获取原先高度+1
        return Math.max(this.left == null ? 0 : this.left.height(), this.right == null ? 0 : this.right.height()) + 1;
    }

    /**
     * 左旋转（以当前结点为根节点）
     */
    private void leftRotate() {
        //1)创建新节点值为当前根节点的值
        Node newNode = new Node(value);
        //2)新结点的左结点为根节点的左结点
        newNode.left = this.left;
        //3)新结点的右结点为根节点的右结点的左结点
        newNode.right = this.right.left;
        //4)当前结点值为当前结点的右结点值
        this.value = this.right.value;
        //5)当前结点的左结点指向新结点
        this.left = newNode;
        //6)当前结点的右结点指向原来右子树的右结点
        this.right = this.right.right;
    }

    /**
     * 右旋转
     */
    public void rightRotate() {
        //1)创建新结点值为当前根节点的值
        Node newNode = new Node(value);
        //2)新结点的右子树为当前右子树
        newNode.right = this.right;
        //3)新结点的左子树为当前根节点的左子树的右子树
        newNode.left = this.left.right;
        //4)当前结点值为当前结点的左子树的值
        this.value = this.left.value;
        //5)当前结点的右子树为新结点
        this.right = newNode;
        //6)当前结点的左子树为当前结点左子树的左子树
        this.left = this.left.left;
    }

    /**
     * 添加结点
     */
    public void add(Node node) {
        if (node == null) {
            return;
        }
        //如果当前节点值大于添加的值
        if (this.value > node.value) {
            //如果左节点不为空就向左递，否则就赋值给左节点
            if (this.left != null) {
                this.left.add(node);
            } else {
                this.left = node;
            }
        } else {
            if (this.right != null) {
                this.right.add(node);
            } else {
                this.right = node;
            }
        }

        if (rightHeight() - leftHeight() > 1) {
            if (right.leftHeight() > right.rightHeight()) {
                right.rightRotate();
            }
            leftRotate();
        }

        if (leftHeight() - rightHeight() > 1) {
            if (left.rightHeight() > left.leftHeight()) {
                left.leftRotate();
            }
            rightRotate();
        }
    }

    /**
     * 中序遍历
     */
    public void midOrder() {
        if (this.left != null) {
            this.left.midOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.midOrder();
        }
    }

    public Node(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Node[" +
                "value=" + value +
                ']';
    }
}
